﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using cludg.Infrastructure.DI;

namespace cludg.Web.Site
{
	public class Site : Core.Modules.ISite
	{
		public string SiteName { get { return "cludg.Web.Site"; } }

		private static Repository.NHibernate.DbDriver GetDbDriver(Properties.Settings settings)
		{
			switch (settings.Database_Driver)
			{
				case "SQLCompact":
					return Repository.NHibernate.DbDriver.SQLCompact;
				case "SQLServer":
					return Repository.NHibernate.DbDriver.SQLServer;
				default:
					return Repository.NHibernate.DbDriver.SQLServer;
			}
		}

		public void Initialize(Core.Modules.ApplicationContext context)
		{
			// Registrations
			context.Container.RegisterInstance<Infrastructure.Core.Settings.ISettings>(Properties.Settings.Default);

			// Repositories
			// NHibernate
			context.Container.Register<
				Repository.NHibernate.Extensions.INHMappingExtensionRegistry,
				Repository.NHibernate.Extensions.NHMappingExtensionRegistry>(DILifetime.PerResolve);
			context.Container.Register<Repository.NHibernate.NHibernateController>(DILifetime.Singleton /*,
				GetDbDriver(Properties.Settings.Default),
				Properties.Settings.Default.Database_ConnectionString,
				context.Container.Resolve<Repository.NHibernate.Extensions.INHMappingExtensionRegistry>()*/);
			//context.Container.Register<Repository.NHibernate.Sessions.INHibernateSessionController,
			//    Web.NHibernate.Sessions.NHibernateSessionController_PerHttpSession>(DILifetime.Singleton);
			context.Container.Register<Repository.NHibernate.Sessions.INHibernateSessionController,
				Web.NHibernate.Sessions.NHibernateSessionController_PerHttpRequest>(DILifetime.Singleton);
			context.Container.Register<
				Repository.Content.IContentRepository<Domain.Content.Stories.Story>,
				Repository.Content.NHibernate.ContentRepository<Domain.Content.Stories.Story>>(DILifetime.Singleton);
			context.Container.Register<
				Repository.Content.IContentRepository<Domain.Content.Stories.Offer>,
				Repository.Content.NHibernate.ContentRepository<Domain.Content.Stories.Offer>>(DILifetime.Singleton);
			context.Container.Register<
				Repository.Content.IContentRepository<Domain.Content.Stories.Comment>,
				Repository.Content.NHibernate.ContentRepository<Domain.Content.Stories.Comment>>(DILifetime.Singleton);
			context.Container.Register<
				Repository.Security.ISecurityRepository,
				Repository.Security.NHibernate.SecurityRepository>(DILifetime.Singleton);
			context.Container.Register<
				Repository.Transactions.ITransactionFactory,
				Repository.Transactions.NHibernate.TransactionFactory>(DILifetime.Singleton);
			context.Container.Register<
				Repository.Content.ITagRepository,
				Repository.Content.NHibernate.TagRepository>(DILifetime.Singleton);
			context.Container.Register<
				Repository.Environment.ISettingRepository,
				Repository.Environment.NHibernate.SettingRepository>(DILifetime.Singleton);
			context.Container.Register<
				Repository.Environment.IHistoryRepository,
				Repository.Environment.NHibernate.HistoryRepository>(DILifetime.Singleton);

			// TODO: тут надо делать VirtualPathProvider и регестрировать его
			context.Container.RegisterInstance<Repository.Images.IImageRepository>(
				new Repository.Images.HttpServerFile.ImageRepository(
					context.Application.Server,
					Properties.Settings.Default.Image_Folder));

			// Services
			context.Container.Register<Services.Content.IStoryService, Services.Content.StoryService>(DILifetime.Singleton);
			context.Container.Register<Services.Content.ICommentService, Services.Content.CommentService>(DILifetime.Singleton);
			context.Container.Register<Services.Content.IOfferService, Services.Content.OfferService>(DILifetime.Singleton);
			context.Container.Register<Services.Content.ITagService, Services.Content.TagService>(DILifetime.Singleton);
			context.Container.Register<Services.Security.IAdministration, Services.Security.Administration>(DILifetime.Singleton);
			context.Container.Register<Services.Security.IMembership, Services.Security.Membership>(DILifetime.Singleton);
			context.Container.Register<Services.Security.IAuthorization, Services.Security.Authorization>(DILifetime.Singleton);
			context.Container.Register<Services.Images.IImageService, Services.Images.ImageService>(DILifetime.Singleton);
			context.Container.Register<Services.Users.IUserService, Services.Users.UserService>(DILifetime.Singleton);
			context.Container.Register<Services.Environment.ISettingService, Services.Environment.SettingService>(DILifetime.Singleton);
			context.Container.Register<Services.Environment.IHistoryService, Services.Environment.HistoryService>(DILifetime.Singleton);

			// Permission providers

			// View model extensions providers

			// Menu providers

			// View register

			// Infrastructure
			context.Container.RegisterInstance<Infrastructure.Mailer.IMailer>(new Infrastructure.Mailer.MailerToFolder("C:\\test\\mails\\"));

			// Bootstrappers
			if (Properties.Settings.Default.Debug_RecreateDatabaseOnRun)
				context.Container.Register<Core.Bootstrappers.IBootstrapper, Bootstrappers.UpdateDatabaseBootstrapper>("UpdateDatabase", DILifetime.PerResolve);
			if (Properties.Settings.Default.Debug_FillTestDataOnRun)
				context.Container.Register<Core.Bootstrappers.IBootstrapper, Bootstrappers.TestDataBootstrapper>("TestData", DILifetime.PerResolve);

			// Filters

			// Modules
			context.Container.Register<Core.Modules.IModule, TestModule.TestModule>("TestModule", DILifetime.Singleton);
			context.Container.Register<Core.Modules.IModule, ShareContentModule.ShareContentModule>("ShareContentModule", DILifetime.Singleton);
			context.Container.Register<Core.Modules.IModule, cludg.Web.RateContentModule.RateContentModule>("RateContentModule", DILifetime.Singleton);
		}
	}
}